using System;
using System.Collections.Generic;
using System.Text;
using LiveStock.Evaluation;
using System.Xml;

namespace LiveStock.RssObjects
{
    static class NewsItemRssReader
    {
        public static void FillNewsItemList(List<NewsItem> theNewsItemList, string rssFeedURL, int evaluationSourceID)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            settings.IgnoreWhitespace = true;
            settings.IgnoreComments = true;

            using (XmlReader reader = XmlReader.Create(rssFeedURL, settings))
            {
                string title = "", description = "", link = "", pubDate = "";
                bool endItem = false;
                bool go = false;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (go)
                        {
                            switch (reader.LocalName.ToLower())
                            {
                                case "item":
                                    endItem = false;
                                    break;
                                case "title":
                                    title = reader.ReadString();
                                    break;
                                case "link":
                                    link = reader.ReadString();
                                    break;
                                case "description":
                                    description = reader.ReadString();
                                    break;
                                case "date":
                                //fall through to pubdate
                                case "pubdate":
                                    pubDate = reader.ReadString();
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    //catch end item
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.LocalName.Equals("item"))
                        {
                            endItem = true;
                        }
                        else if (reader.LocalName.Equals("image"))
                        {
                            go = true;
                        }
                    }

                    if (endItem && reader.NodeType != XmlNodeType.Whitespace)
                    {
                        NewsItem newNewsItem = new NewsItem(ReplaceXMLEntityWithCharacter(title.Trim()), description, link, Convert.ToDateTime(pubDate),
                            evaluationSourceID);
                        //Remove duplicates news items returned
                        bool duplicate = false;
                        foreach (NewsItem newsItem in theNewsItemList)
                        {
                            if (newsItem.Equals(newNewsItem))
                            {
                                duplicate = true;
                                break;
                            }
                        }
                        if (!duplicate)
                        {
                            theNewsItemList.Add(newNewsItem);
                        }                            
                    }
                }
            }
        }

        public static string ReplaceXMLEntityWithCharacter(string orig)
        {
            StringBuilder sb = new StringBuilder(orig);
            sb.Replace("&amp;", "&");
            sb.Replace("&#38;#38;", "&");
            sb.Replace("&lt;", "<");
            sb.Replace("&#38;#60;", "<");
            sb.Replace("&gt;", ">");
            sb.Replace("&#62;", ">");
            sb.Replace("&quot;", "\"");
            sb.Replace("&#34;", "\"");
            sb.Replace("&apos;", "'");
            sb.Replace("&#39;", "'");
            return sb.ToString();
        }
    }
}
